#region Using
using System;
using System.Collections.Generic;
using System.Text;
using System.Data;
using System.Data.SqlClient;
using CY.GFive.Core.Business;
using CY.Utility.DBUtility;
#endregion Using

namespace CY.GFive.DALProviders.SqlServerProvider
{
	public class AssetProvider : CY.GFive.Core.Providers.DALProvider.IAssetProvider
	{
		#region Sql
		private static readonly string SqlSelectAsset = "SELECT [Id], [AssetType],[AssetItemName],[UnitValue],[PurchasingDate],[RegisterDate],[Num],[TotalValue] FROM [Asset] WHERE  [Id]=@Id";

		private static readonly string SqlInsertAsset = "INSERT INTO [Asset]([AssetType],[AssetItemName],[UnitValue],[PurchasingDate],[RegisterDate],[Num],[TotalValue]) values(@AssetType,@AssetItemName,@UnitValue,@PurchasingDate,@RegisterDate,@Num,@TotalValue);SELECT SCOPE_IDENTITY();";

		private static readonly string SqlUpdateAsset = "UPDATE [Asset] SET [AssetType] = @AssetType,[AssetItemName] = @AssetItemName,[UnitValue] = @UnitValue,[PurchasingDate] = @PurchasingDate,[RegisterDate] = @RegisterDate,[Num] = @Num,[TotalValue] = @TotalValue WHERE  [Id]=@Id";

		private static readonly string SqlDeleteAsset = "DELETE [Asset] WHERE  [Id]=@Id";

		private static readonly string SqlGetAllAsset = "SELECT [Id], [AssetType],[AssetItemName],[UnitValue],[PurchasingDate],[RegisterDate],[Num],[TotalValue] FROM [Asset]";
		#endregion

		#region IAssetProvider Members
		public Core.Business.Asset Select(int id)
		{
			SqlServerUtility sql = new SqlServerUtility();

			sql.AddParameter("@Id", SqlDbType.Int, id);
			SqlDataReader reader = sql.ExecuteSqlReader(SqlSelectAsset);

			if (reader != null && !reader.IsClosed && reader.Read())
			{
				Core.Business.Asset asset = new Core.Business.Asset();

				if (!reader.IsDBNull(0)) asset.Id = reader.GetInt32(0);
				if (!reader.IsDBNull(1)) asset.AssetType = reader.GetString(1);
				if (!reader.IsDBNull(2)) asset.AssetItemName = reader.GetString(2);
				if (!reader.IsDBNull(3)) asset.UnitValue = reader.GetDecimal(3);
				if (!reader.IsDBNull(4)) asset.PurchasingDate = reader.GetDateTime(4);
				if (!reader.IsDBNull(5)) asset.RegisterDate = reader.GetDateTime(5);
				if (!reader.IsDBNull(6)) asset.Num = reader.GetInt32(6);
				if (!reader.IsDBNull(7)) asset.TotalValue = reader.GetDecimal(7);

				return asset;
			}
			else
			{
				if (reader != null && !reader.IsClosed)
					reader.Close();

				return null;
			}
		}

		public void Insert(Core.Business.Asset asset)
		{
			SqlServerUtility sql = new SqlServerUtility();

			sql.AddParameter("@AssetType", SqlDbType.NVarChar, asset.AssetType);
			sql.AddParameter("@AssetItemName", SqlDbType.NVarChar, asset.AssetItemName);
			sql.AddParameter("@UnitValue", SqlDbType.Decimal, asset.UnitValue);
			sql.AddParameter("@PurchasingDate", SqlDbType.DateTime, asset.PurchasingDate);
			sql.AddParameter("@RegisterDate", SqlDbType.DateTime, asset.RegisterDate);
			sql.AddParameter("@Num", SqlDbType.Int, asset.Num);
			sql.AddParameter("@TotalValue", SqlDbType.Decimal, asset.TotalValue);
			SqlDataReader reader = sql.ExecuteSqlReader(SqlInsertAsset);

			if (reader != null && !reader.IsClosed && reader.Read())
			{
				if (!reader.IsDBNull(0)) asset.Id = int.Parse(reader.GetDecimal(0).ToString());

				reader.Close();
			}
			else
			{
				if (reader != null && !reader.IsClosed)
					reader.Close();
			}
		}

		public void Update(Core.Business.Asset asset)
		{
			SqlServerUtility sql = new SqlServerUtility();

			sql.AddParameter("@AssetType", SqlDbType.NVarChar, asset.AssetType);
			sql.AddParameter("@AssetItemName", SqlDbType.NVarChar, asset.AssetItemName);
			sql.AddParameter("@UnitValue", SqlDbType.Decimal, asset.UnitValue);
			sql.AddParameter("@PurchasingDate", SqlDbType.DateTime, asset.PurchasingDate);
			sql.AddParameter("@RegisterDate", SqlDbType.DateTime, asset.RegisterDate);
			sql.AddParameter("@Num", SqlDbType.Int, asset.Num);
			sql.AddParameter("@TotalValue", SqlDbType.Decimal, asset.TotalValue);
			sql.AddParameter("@Id", SqlDbType.Int, asset.Id);
			sql.ExecuteSql(SqlUpdateAsset);
		}

		public void Delete(Core.Business.Asset asset)
		{
			SqlServerUtility sql = new SqlServerUtility();

			sql.AddParameter("@Id", SqlDbType.Int, asset.Id);
			sql.ExecuteSql(SqlDeleteAsset);
		}

		public IList<Core.Business.Asset> GetAllAsset()
		{
			IList<Core.Business.Asset> assetlist = new List<Core.Business.Asset>();
			SqlServerUtility sql = new SqlServerUtility();

			SqlDataReader reader = sql.ExecuteSqlReader(SqlGetAllAsset);

			if(reader != null)
			{
				while(reader.Read())
				{
					Core.Business.Asset asset = new Core.Business.Asset();

					if (!reader.IsDBNull(0)) asset.Id = reader.GetInt32(0);
					if (!reader.IsDBNull(1)) asset.AssetType = reader.GetString(1);
					if (!reader.IsDBNull(2)) asset.AssetItemName = reader.GetString(2);
					if (!reader.IsDBNull(3)) asset.UnitValue = reader.GetDecimal(3);
					if (!reader.IsDBNull(4)) asset.PurchasingDate = reader.GetDateTime(4);
					if (!reader.IsDBNull(5)) asset.RegisterDate = reader.GetDateTime(5);
					if (!reader.IsDBNull(6)) asset.Num = reader.GetInt32(6);
					if (!reader.IsDBNull(7)) asset.TotalValue = reader.GetDecimal(7);

					asset.MarkOld();
					assetlist.Add(asset);
				}
				reader.Close();
			}
			return assetlist;
		}
		#endregion
	}
}
